import os, glob

class FindSuffixFile(object):
    def __init__(self, addr='', suffix=''):
        """
        Init the addr and suffix
        :param addr: address of folder
        :param suffix: the file suffix for searching
        """
        self.addr = addr
        self.suffix = suffix

        #save file name of the search result
        self.search_result_list = []

    def set_addr(self, addr):
        """
        Reset address
        :param addr:address
        """
        self.addr = addr

    def set_suffix(self, suffix):
        """
        Reset suffix for searching
        :param suffix: file suffix
        """
        self.suffix = suffix

    def get_search_result(self):
        """
        :return: Search result
        """
        return self.search_result_list

    def start_search(self):
        """
        Search the certain suffix file
        """
        #get file name
        os.chdir(self.addr)
        self.search_result_list = glob.glob(r'*.{}'.format(self.suffix))

        #combine path and name
        tmp_list = []
        for i in range(len(self.search_result_list)):
            tmp_list.append(self.addr + '\\' + self.search_result_list[i])
        self.search_result_list = tmp_list


class DeleteFile(object):
    def __init__(self, filelist):
        """
        Init the file list for deleting
        :param filelist: same suffix file name list
        """
        self.filelist = filelist

    def start_delete(self):
        """
        Delete all files with provided suffix
        """
        for i in range(len(self.filelist)):
            os.remove(self.filelist[i])


class FindSuffixFile_DF(FindSuffixFile):
    def __init__(self, addr='', suffix=''):
        super().__init__(addr, suffix)

    def start_delete(self):
        """
        Delete all files with provided suffix
        """
        for i in range(len(self.search_result_list)):
            os.remove(self.search_result_list[i])